package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Courseorder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.mapper.CourseorderMapper;
import cn.wolfcode.crm.query.CourseQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class ICourseServiceImpl implements ICourseService {
    @Autowired
    private CourseorderMapper courseorderMapper;
    @Override
    public PageInfo listAll(CourseQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Courseorder> courseorders = courseorderMapper.selectAll(qo);
        String json = courseorders.get(0).getJson();
        System.out.println(json);
        return new PageInfo(courseorders);
    }

    @Override
    public void saveOrUpdate(String id, String customerId, String courseId, String money) {
        if(!StringUtils.isEmpty(id)){
            //修改操作
            Courseorder courseorder = courseorderMapper.selectByPrimaryKey(Long.parseLong(id));
            courseorder.getCustomer().setId(Long.parseLong(customerId));
            courseorder.getCourse().setId(Long.parseLong(courseId));
            courseorder.setMoney(Long.parseLong(money));
            courseorderMapper.updateByPrimaryKey(courseorder);
        }else {
            //添加操作
            Courseorder courseorder = new Courseorder();
            courseorder.setInputTime(new Date());//设置交易时间
            Customer customer = new Customer();
            customer.setId(Long.valueOf(customerId));
            courseorder.setCustomer(customer);
            SystemDictionaryItem course = new SystemDictionaryItem();
            course.setId(Long.valueOf(courseId));
            courseorder.setCourse(course);
            courseorder.setMoney(Long.valueOf(money));
            courseorderMapper.insert(courseorder);
        }
    }
}
